//
//  MultipleIPSettingController.m
//  Estate
//
//  Created by heyong on 2017/3/31.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "MultipleIPSettingController.h"

@interface MultipleIPSettingController (){
    //全局model 用来确定单选
    MultipleIPModel *flagModel;
}

@end

@implementation MultipleIPSettingController

- (UITableView *)tableView{
    if(!_tableView){
        _tableView=[[UITableView alloc]init];
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator=NO;
        [_tableView registerNib:[UINib nibWithNibName:@"MuitipleIPCell" bundle:nil] forCellReuseIdentifier:@"ipCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ipAddBtnCell" bundle:nil] forCellReuseIdentifier:@"addBtnCell"];
    }
    return _tableView;
}

- (UIView *)topView{
    if(!_topView){
        _topView=[[UIView alloc]initWithFrame:CGRECTS(0, 0, 375, 165)];
        _topView.backgroundColor=[UIColor colorWithRed:0.39 green:0.73 blue:0.65 alpha:1.00];
    }
    return _topView;
}
- (UILabel *)ipLab{
    if(!_ipLab){
        _ipLab=[[UILabel alloc]init];
    }
    return _ipLab;
}
- (UILabel *)detailLab{
    if(!_detailLab){
        _detailLab=[[UILabel alloc]init];
    }
    return _detailLab;
}

-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createTopView];
    //    DEF_PERSISTENT_REMOVE(@"Multip");
    [self loadData];
    [self.view addSubview:self.topView];
}

#pragma mark --设置数据
-(void)loadData{
    //反序列化
    NSData * data=DEF_PERSISTENT_GET_OBJECT(@"Multip");
    
    NSArray * array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    self.dataArray = [[NSMutableArray alloc]initWithArray:array];
    
    //设置顶部视图数据
    for (MultipleIPModel *model in self.dataArray) {
        bool issel=model.isSel;
        if(issel){
            NSString *ipdetail=@"";
            if(model.port){
                if([model.port isEqualToString:@""]){
                    ipdetail=model.ip;
                }else{
                    ipdetail=[NSString stringWithFormat:@"%@:%@",model.ip,model.port];
                }
            }else{
                ipdetail=[NSString stringWithFormat:@"%@",model.ip];
            }
            self.ipLab.text=ipdetail;
            if(model.remark){
                self.detailLab.text=[NSString stringWithFormat:@"备注：%@", DEFISNUL(model.remark)];
            }else{
                self.detailLab.text=@"备注：暂无";
            }
        }
    }
    
    //当前ip
    NSLog(@"当前ip:%@",DEFIP);
    
}

#pragma mark --创建页面
- (void)createTopView{
    UILabel *title=[[UILabel alloc]init];
    title.text=@"服务器地址";
    title.font=[UIFont systemFontOfSize:14.];
    title.textColor=[UIColor whiteColor];
    title.textAlignment=1;
    [self.topView addSubview:title];
    WEAKSELF
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(50*BIZ);
        make.centerX.mas_equalTo(weakSelf.topView.mas_centerX);
        make.height.offset(15*BIZ);
        make.width.offset(375*BIZ);
    }];
    [self.topView addSubview:self.ipLab];
    
    [self.topView addSubview:self.ipLab];
    self.ipLab.font=[UIFont systemFontOfSize:25.];
    [self.ipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(title.mas_bottom).offset(10*BIZ);
        make.centerX.mas_equalTo(weakSelf.topView.mas_centerX);
        make.height.offset(30*BIZ);
        make.width.offset(375*BIZ);
    }];
    self.ipLab.textAlignment=1;
    self.ipLab.textColor=[UIColor whiteColor];
    self.ipLab.text=@"暂无";
    
    [self.topView addSubview:self.detailLab];
    self.detailLab.font=[UIFont systemFontOfSize:12.];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.ipLab.mas_bottom).offset(8*BIZ);
        make.centerX.mas_equalTo(weakSelf.topView.mas_centerX);
        make.height.offset(15*BIZ);
        make.width.offset(375*BIZ);
    }];
    self.detailLab.textColor=[[UIColor whiteColor] colorWithAlphaComponent:0.7];
    self.detailLab.textAlignment=1;
    self.detailLab.text=@"备注：暂无";
    
    UIButton *backBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 20, 30, 30)];
    [backBtn setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal
     ];
    [backBtn addTarget:self action:@selector(backLoging) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:backBtn];
    
    //保存按钮
    UIButton *saveBtn=[[UIButton alloc]init];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    saveBtn.titleLabel.font=[UIFont systemFontOfSize:14.];
    [saveBtn addTarget:self action:@selector(saveIp) forControlEvents:UIControlEventTouchUpInside];
    
    [self.topView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.right.offset(-3);
        make.height.offset(40);
        make.width.offset(60);
    }];
    
    UIView *topView_2=[[UIView alloc]init];
    [self.view addSubview:topView_2];
    [topView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(164*BIZ);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(-1);
        make.width.offset(376*BIZ);
        make.height.offset(36*BIZ);
    }];
    topView_2.layer.borderWidth=1;
    topView_2.layer.borderColor=[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00].CGColor;
    
    UIImageView *img=[[UIImageView alloc]init];
    [topView_2 addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15*BIZ);
        make.top.offset(10*BIZ);
        make.height.offset(15*BIZ);
        make.width.offset(15*BIZ);
    }];
    img.image=[UIImage imageNamed:@"dizhi1"];
    
    UILabel *tit2=[[UILabel alloc]init];
    tit2.text=@"选择服务器地址";
    tit2.textColor=[UIColor colorWithRed:0.39 green:0.73 blue:0.65 alpha:1.00];
    tit2.font=[UIFont systemFontOfSize:13.];
    [topView_2 addSubview:tit2];
    [tit2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(img.mas_right).offset(8*BIZ);
        make.top.mas_equalTo(img.mas_top);
        make.height.offset(15*BIZ);
        make.width.offset(300);
    }];
    //tableView
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView_2.mas_bottom).offset(0);
        make.width.offset(375*BIZ);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).offset(0);
    }];
}


#pragma mark -UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count+1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row=indexPath.row;
    //最后一行
    if(row==self.dataArray.count){
        return 110*BIZ;
    }else{
        return 65*BIZ;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    NSInteger row=indexPath.row;
    //最后一行 添加按钮
    if(row==self.dataArray.count){
        ipAddBtnCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addBtnCell" forIndexPath:indexPath];
        IpDetailController *addIpCtr=[[IpDetailController alloc]init];
        cell.addBlick = ^{
            [weakSelf presentViewController:addIpCtr animated:YES completion:^{
            }];
        };
        return cell;
        
    }else{
        MuitipleIPCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ipCell" forIndexPath:indexPath];
        
        MultipleIPModel *model=self.dataArray[row];
        //设置选中状态
        cell.checkBtn.selected=model.isSel;
        //记录选中ip model
        if(model.isSel){
            flagModel=model;
        }
        NSString *ipdetail=@"";
        if(model.port){
            if([model.port isEqualToString:@""]){
                ipdetail=model.ip;
            }else{
                ipdetail=[NSString stringWithFormat:@"%@:%@",model.ip,model.port];
            }
        }else{
            ipdetail=model.ip;
        }
        cell.ipTextLab.text=ipdetail;
        
        cell.ipDetailLab.text=model.remark;
        
        //     __block MuitipleIPCell *cellBlock = cell;
        cell.selBlick = ^{
            //清除上一个选中ip的选中状态
            flagModel.isSel=NO;
            //设置新的ip为选中状态
            model.isSel=YES;
            //顶部显示数据
            self.ipLab.text=ipdetail;
            if(model.remark){
                self.detailLab.text=[NSString stringWithFormat:@"备注：%@", DEFISNUL(model.remark)];
            }else{
                self.detailLab.text=@"备注：暂无";
            }
            [weakSelf.tableView reloadData];
        };
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row= indexPath.row;
    if(row<self.dataArray.count){
        IpDetailController *addIpCtr=[[IpDetailController alloc]init];
        addIpCtr.first=NO;
        MultipleIPModel *model=self.dataArray[row];
        addIpCtr.ipModel=[model mutableCopy];
        addIpCtr.row=row;
        [self presentViewController:addIpCtr animated:YES completion:^{
        }];
    }
}

#pragma mark -- 返回
-(void)backLoging{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark -保存

-(void)saveIp{
    NSData * saveData = [NSKeyedArchiver archivedDataWithRootObject:self.dataArray];
    DEF_PERSISTENT_SET_OBJECT(saveData,@"Multip");
    for (MultipleIPModel *model in self.dataArray) {
        bool issel=model.isSel;
        if(issel){
            NSString *ipdetail=@"";
            if(model.port){
                if([model.port isEqualToString:@""]){
                    ipdetail=model.ip;
                }else{
                    ipdetail=[NSString stringWithFormat:@"%@:%@",model.ip,model.port];
                }
            }else{
                ipdetail=model.ip;
            }
            //持久化此ip地址
            DEF_PERSISTENT_SET_OBJECT(ipdetail, @"SAVEIP");
        }
    }
    
    if(DEFIP&&![DEFIP isEqualToString:@""]){
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
